package com.scheduled.controller;

import com.scheduled.pojo.ScheduledRequest;
import com.scheduled.pojo.dto.BaseResponseDTO;
import com.scheduled.service.ScheduledService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.UnknownHostException;

/**
 * 定时任务访问控制层
 *
 * @author : z_sz
 * @date : 2020-08-10 10:18
 **/
@RestController
@RequestMapping("/scheduled")
public class ScheduledController {

    /**
     * 延迟执行服务层
     */
    @Autowired
    ScheduledService scheduledService;

    /**
     * 延迟任务设置
     * @param scheduledRequest 延迟任务设置实体
     * @return 操作结果
     * @throws UnknownHostException ip地址不存在异常
     */
    @PostMapping("/delayExecute")
    public String delayExecute(@Valid @RequestBody ScheduledRequest scheduledRequest) throws UnknownHostException {
        return scheduledService.delayExecute(scheduledRequest);
    }

    /**
     * 取消已经设置的延迟任务
     * @param id 任务唯一标识/任务主键
     * @return 操作结果
     * @throws UnknownHostException ip地址不存在异常
     */
    @GetMapping("/delayCancel")
    public BaseResponseDTO delayExecute(@RequestParam("id")String id) throws UnknownHostException {
        return scheduledService.delayCancel(id);
    }

}
